﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ContractorTimesheet.Infrastructure
{
    public class Calendar
    {
        public static int CurrentMonthBusinessDays
        {
            get
            {
                DateTime firstDayOfMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);

                DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);

                return CalculateBusinessDays(firstDayOfMonth, lastDayOfMonth);
            }
        }

        private static int CalculateBusinessDays(DateTime startDate, DateTime endDate)
        {
            if (DateTime.Compare(startDate, endDate) == 1)
                return 0;

            int noOfBusinessDays = 0;

            while (DateTime.Compare(startDate, endDate) <= 0)
            {
                if (startDate.DayOfWeek != DayOfWeek.Saturday && startDate.DayOfWeek != DayOfWeek.Sunday)
                    noOfBusinessDays += 1;

                startDate = startDate.AddDays(1);
            }

            return noOfBusinessDays;
        }

    }
}